import Vue from 'vue';
import Router from 'vue-router';
import Home from '@/home/Home';
import Login from '@/pages/Login';
import Register from '@/pages/Register';
import Admin from '@/pages/Admin';
import ManagerMain from '@/manager/ManagerMain';
import TeacherList from '@/manager/components/TeacherList';
import TeachAreaSetting from '@/manager/components/TeachAreaSetting';
import SystemData from '@/manager/SystemData';
import ClassroomList from '@/manager/components/ClassroomList';
import TeachBuildingList from '@/manager/components/TeachBuildingList';
import CourseTable from '@/manager/components/CourseTable';
import StudentCourseTable from '@/home/components/CourseTable';
import EmptyClassroom from '@/home/components/EmptyClassroom';
import StudentCenter from '@/home/components/Center';
import StudentPassword from '@/home/components/Password';
import CourseInfoList from '@/manager/components/CourseInfoList';
import StudentList from '@/manager/components/StudentList';
import ClassTaskList from '@/manager/components/ClassTaskList';
import ClassManager from '@/manager/components/ClassManager';
import UpdatePass from '@/pages/components/UpdatePass';
import Help from '@/manager/components/Help';
import StudentMain from '@/home/StudentMain';
import ClassroomSchedule from '@/manager/components/ClassroomSchedule.vue';
import TeacherSchedule from '@/manager/components/TeacherSchedule.vue';

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home,
      meta: { 
        noRequireAuth: true
      },
    },
    {
      path: '/student/login',
      name: 'Login',
      component: Login,
      meta: { 
        noRequireAuth: true
      },
    },
    {
      path: '/student/register',
      name: 'Register',
      component: Register,
      meta: { 
        noRequireAuth: true
      },
    },
    {
      path: '/admin/login',
      name: 'AdminLogin',
      component: Admin,
      meta: { 
        noRequireAuth: true
      },
    },
    {
      path: '/student',
      name: 'Student',
      component: StudentMain,
      meta: { 
        noRequireAuth: true
      },
      children: [
        { path: 'coursetable', component: CourseTable },
        { path: 'classroom-schedule', component: ClassroomSchedule },
        { path: 'emptyclassroom', component: EmptyClassroom },
        { path: 'center', component: StudentCenter },
        { path: 'password', component: StudentPassword },
        {
          path: '',
          name: 'CourseList-default',
          component: StudentCourseTable
        }
      ]
    },
    {
      path: '/admin',
      name: 'Admin',
      component: ManagerMain,
      children: [
        {
          path: '/help',
          name: 'Help',
          component: Help
        },
        {
          path: '/updatepass',
          name: 'UpdatePass',
          component: UpdatePass
        },
        {
          path: '/systemdata',
          name: 'SystemData',
          component: SystemData
        },
        {
          path: '/teacherlist',
          name: 'TeacherList',
          component: TeacherList
        },
        {
          path: '/studentlist',
          name: 'StudentList',
          component: StudentList
        },
        {
          path: '/teachbuildinglist',
          name: 'TeachBuildingList',
          component: TeachBuildingList
        },
        {
          path: '/classroomlist',
          name: 'ClassroomList',
          component: ClassroomList
        },
        {
          path: '/coursetable',
          name: 'CourseTable',
          component: CourseTable
        },
        {
          path: '/courseinfolist',
          name: 'CourseInfoList',
          component: CourseInfoList
        },
        {
          path: '/classtasklist',
          name: 'ClassTaskList',
          component: ClassTaskList
        },
        {
          path: '/classmanager',
          name: 'ClasssManager',
          component: ClassManager
        },
        {
          path: '/setteacharea',
          name: 'TeachAreaSetting',
          component: TeachAreaSetting
        },
        {
          path: '/classroom-schedule',
          name: 'ClassroomSchedule',
          component: ClassroomSchedule
        },
        {
          path: '/teacher-schedule',
          name: 'TeacherSchedule',
          component: TeacherSchedule
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (!to.matched.some(res => res.meta.noRequireAuth)) {// 判断是否需要登录权限
    if (window.localStorage.getItem('teacher') || window.localStorage.getItem("admin") || window.localStorage.getItem("student")) {// 判断是否登录
      next()
    } else {
      next({
        path: '/',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

export default router